<?php
/*
checking version control

*/
include('ip2locationlite.class.php');
 //$current_domain= $_SERVER['HTTP_HOST'];
$current_domain="home24.com";
$host_names = explode(".", $current_domain);
$domain = $host_names[0];
//echo "domain=$domain.<br>";
//echo "current domain is $current_domain";  //get the current domain of user

if(!(strcmp($current_domain,"home24.com"))){    //check if its browsing home24
	$ip = gethostbyname($current_domain);       //gethostname for URL
	if(!(strcmp($current_domain,$ip))) {       //gethostbyname is NOT SUCCESSFUL
		echo "DNS Lookup failed";
	}
	else {                     //if gethostbyname is SUCCESSFUL
		/*if (!cookie)
		     run API
		else 
		    useCookie;
		*/
			//Load the class
			$ipLite = new ip2location_lite;               
			$ipLite->setKey('9106ff06cf24e0a0ce6d190cd73b5b0758e5a6f5e5adae60ff6d79c63d3a07d8');   //Input the API key, obtained after registering on IPinfoDB
			//Get errors and locations
			$locations = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);  // getCountry info 
			//$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);   //Uncomment to getCity instead of getCountry
		  //run API 
			$errors = $ipLite->getError();
 				//Getting the result
				echo "<p>\n";
				echo "<strong>First result</strong><br />\n";

				if (!empty($locations) && is_array($locations)) {   
				  foreach ($locations as $field => $val) {
					echo $field . ' : ' . $val . "<br />\n";   //Display 

					if(!(strcmp($field,"countryCode"))){    
				   $countryC=$val;							//Extract countryCode from API
					}
				  }
				}
				//checking for various country code using Switch Case
				switch($countryC){
					case 'SG':
						//$r="http://www.".$domain.".".$countryC;
						$r="http://www.".$domain.".".$countryC;
						//header( "Location:".$r ) ;              //browser redirection 
						print $r;
						break;
					case 'MY':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'VN':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'DE':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'ID':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'IN':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					default:
						echo "error";
				}
				//print "\n***The countryCode for given IP address is =$countryC***\n";          
				echo "</p>\n";

							

					}
				}
				else {
					echo "API is not running";
					//DO NOT run API
				 }
			 
//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
  foreach ($errors as $error) {
    echo var_dump($error) . "<br /><br />\n";
  }
} else {
  echo "No errors" . "<br />\n";
}
echo "</p>\n";